package servlets;

import beans.Examen;
import beans.Promotion;
import dao.DAOFactory;
import dao.ExamenDAO;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import beans.Utilisateur;
import dao.AffectePromoUtilisateurDAO;
import dao.NoteDAO;
import dao.PromotionDAO;
import dao.UtilisateurDAO;
import forms.SaisirNoteForm;
import java.util.Enumeration;
import javax.servlet.annotation.WebServlet;


@WebServlet(name = "SaisirNote", urlPatterns = "/saisirNote")
public class SaisirNote extends HttpServlet {
    private final static String VUE             = "/WEB-INF/saisirNote.jsp";
    public static final String CONF_DAO_FACTORY = "daofactory";
    
    public static final String ATT_NOTE         = "note";
    public static final String ATT_FORM         = "form";
    
   private ExamenDAO examenDAO;
   private UtilisateurDAO utilisateurDAO;
   private PromotionDAO promotionDAO;
   private NoteDAO noteDAO;
   private List<Utilisateur> listeU;
   
    @Override
    public void init() throws ServletException {
        /* Récupération d'une instance de nos DAO */
        this.noteDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getNoteDao(); 
        this.promotionDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getPromotionDao(); 
        this.utilisateurDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getUtilisateurDao(); 
        this.examenDAO = ( (DAOFactory) getServletContext().getAttribute( CONF_DAO_FACTORY ) ).getExamenDao(); 
        }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
              
      String exaS = request.getParameter ("exam");
      if (exaS!=null){
          Integer exa = Integer.parseInt(exaS);
          Examen examen= examenDAO.trouverExamen(exa);
          Promotion promo = promotionDAO.trouverPromotionParExamen(examen.getId_exam());
          listeU = utilisateurDAO.trouverUtilisateursParPromotion(promo.getId_promo());
           
       request.setAttribute("examen", examen );
       request.setAttribute("promo", promo);
       request.setAttribute("etudiants", listeU);
      }
       this.getServletContext().getRequestDispatcher( VUE ).forward( request, response );
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        /* Préparation de l'objet formulaire */
        SaisirNoteForm form = new SaisirNoteForm( noteDAO );
 
        /* Traitement de la requête et récupération du bean en résultant */
        form.enregistrerNotes(request);
        /* Stockage dans l'objet request */
        request.setAttribute( ATT_FORM, form );
        
        response.sendRedirect(request.getContextPath() + "/saisirNote?exam="+request.getParameter("examen"));
    }

   
}
